package casa.depesos.app.com.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.support.toast.ToastUtil;

import java.util.List;

import casa.depesos.app.com.App;
import casa.depesos.app.com.R;
import casa.depesos.app.com.databinding.DayItemBinding;
import casa.depesos.app.com.frag.TakeFragment;
import casa.depesos.app.com.http.data.Fee;

public class DayAdapter extends RecyclerView.Adapter {

    List<Fee> list;
    int select = -1;
    TakeFragment fragment;

    public DayAdapter(TakeFragment fragment, List<Fee> list) {
        this.list = list;
        this.fragment = fragment;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).isValid) {
                    select = i;
                    break;
                }
            }
        }
    }

    public Fee getCurrent() {
        if (select >= 0) {
            return list.get(select);
        }
        return null;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        DayItemBinding binding = DayItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder) {
            ((Holder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        DayItemBinding binding;

        Holder(DayItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            Fee item = list.get(p);
            if (item.isValid) {
                if (p == select) {
                    binding.getRoot().setTextColor(Color.WHITE);
                    binding.getRoot().setBackgroundResource(R.drawable.right_btn);
                } else {
                    binding.getRoot().setTextColor(0xff71D561);
                    binding.getRoot().setBackgroundResource(R.drawable.green_stroke_bg);
                }
                binding.getRoot().setOnClickListener(v -> {
                    select = getBindingAdapterPosition();
                    notifyDataSetChanged();
                    fragment.onFeedChange(item);
                });
            } else {
                binding.getRoot().setTextColor(Color.WHITE);
                binding.getRoot().setBackgroundResource(R.drawable.day_disable);
                binding.getRoot().setOnClickListener(v -> {
                    ToastUtil.showShortToast(App.getApp(), "Todavía no está abierto, estad atentos");
                });
            }
            binding.getRoot().setText((item.day + 1) + " días");
        }
    }
}
